package com.bruce.job;

import com.bruce.service.OrderService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description: 任务类，实现JOB接口，重写其中的方法
 * 在job类中获取spring的对象
 */
public class MyJob implements Job {
    /*@Autowired
    private OrderService orderService;*/ //这里面是不能注入的,空指针报错

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String time=simpleDateFormat.format(new Date());
        System.out.println("各位,下午好！保护好你们的身体！"+time);

        //这样就可以调用业务层方法了
        ApplicationContext applicationContext = (ApplicationContext) context.getJobDetail().getJobDataMap().get("applicationContext");
        OrderService bean = applicationContext.getBean(OrderService.class);
        bean.sayhi();
    }
}
